package com.leetcode.partition14;

import com.leetcode.common.TreeNode;

/**
 * @author rkc
 * @date 2022/8/19 9:13
 */
public class LC1302层数最深叶子节点的和 {

    private static final int N = 10010;
    private int[] sum = new int[N];
    private int max = 0;

    public int deepestLeavesSum(TreeNode root) {
        dfs(root, 1);
        return sum[max];
    }

    private void dfs(TreeNode u, int level) {
        if (u == null) return;
        sum[level] += u.val;
        max = Math.max(max, level);
        dfs(u.left, level + 1);
        dfs(u.right, level + 1);
    }
}
